/*	Copyright 2007 - Xavier Baro (xbaro@cvc.uab.cat)

	This file is part of eapmlib.

    Eapmlib is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or any 
	later version.

    Eapmlib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "Classifier.h"

Evolutive::CClassifier::CClassifier(void):m_FilterActive(false)
{
	m_ImageSize.width=-1;
	m_ImageSize.height=-1;
}

Evolutive::CClassifier::~CClassifier(void)
{	
}


bool Evolutive::CClassifier::IsValid(CChromosome *C)
{
	// By default all parameters instantiation are good
	return true;
}
void Evolutive::CClassifier::SetFilter(bool Actived)
{
	// Store the given option
	m_FilterActive=Actived;
}

void Evolutive::CClassifier::SetFilter(bool Actived, CvSize ImageSize,bool UseColor)
{
	// Store the values
	m_ImageSize=ImageSize;
	m_UseColor=UseColor;
	m_FilterActive=Actived;
}

void Evolutive::CClassifier::SetSize(int Width, int Height)
{
	// Store the values
	m_ImageSize.width=Width;
	m_ImageSize.height=Height;
}

CvSize Evolutive::CClassifier::GetSize(void)
{
	return m_ImageSize;
}

void Evolutive::CClassifier::Load(std::string FileName)
{
	fstream ifs;
		
	// Open the input file
	ifs.open(FileName.data(),ios_base::in | ios_base::binary);
	if(ifs.fail())
		throw CEvolutiveLibException("Cannot open input file",__FILE__,__LINE__,"Load");

	// Recover the data
	Serialize(ifs,false);

	// Close the file
	ifs.close();
}

void Evolutive::CClassifier::Save(std::string FileName)
{
	fstream ofs;
	
	// Create the output file
	ofs.open(FileName.data(),ios_base::out | ios_base::trunc | ios_base::binary);
	if(ofs.fail())
		throw CEvolutiveLibException("Cannot open output file",__FILE__,__LINE__,"Save");

	// Save the data
	Serialize(ofs,true);

	// Close the file
	ofs.flush();
	ofs.close();
}

void Evolutive::CClassifier::Serialize(std::fstream &fs,bool Saving)
{
	if(Saving)
	{
		// Save the data to a file
		// Save the class attributes
		fs.write((char*)&m_ImageSize,sizeof(m_ImageSize));
		fs.write((char*)&m_UseColor,sizeof(bool));	
		fs.write((char*)&m_FilterActive,sizeof(bool));	
	}
	else
	{
		// Load the data from a file
		// Load the class attributes
		fs.read((char*)&m_ImageSize,sizeof(m_ImageSize));
		fs.read((char*)&m_UseColor,sizeof(bool));	
		fs.read((char*)&m_FilterActive,sizeof(bool));			
	}	
}
